/**
 * Created by Administrator on 2017/5/18.
 */
function init(){
	var page = 1;
	var rows = 10;
	/**
	 * 新增分类
	 * 
	 */
	$('#btn-add').click(function(e){
		var level = 1;
		var parentId = '';
		layer.prompt({title: '新增商品分类', formType: 0}, function(categoryName, index){
			$.ajax({
				type: "post",
				url: '/diconet/productCategory/save',
				data: {
					parentId : parentId,
					categoryName : categoryName,
					level : level
				},
				dataType: 'json',
				success:function(data){
					if(data.success == 'T'){
						layer.close(index);
						loadComment(page,rows,categoryName);
					}else{
						layer.msg(data.message, {icon: 5,time: 1000});
					}
				},error: function(data){
					layer.msg('新增失败', {icon: 5,time: 1000});
				}
			});
		});
	});
	
	
	/**
	 * 新增子类方法
	 */
	$(".type-item").on('click','.add',function(e){
		var level = parseInt($(e.target)[0].name) + 1;
		var parentId = $(this).parent().parent().attr('data-id');
		layer.prompt({title: '新增商品分类子类', formType: 0}, function(categoryName, index){
			$.ajax({
				type: "post",
				url: '/diconet/productCategory/save',
				data: {
					parentId : parentId,
					categoryName : categoryName,
					level : level
				},
				dataType: 'json',
				success: function(data){
					if(data.success == 'T'){
						layer.close(index);
						var categoryName = $("#categoryName").val();
				    	loadComment(page,rows,categoryName);
					}else{
						layer.msg(data.message, {icon: 5,time: 1000});
					}
				},
				error: function(data){
					layer.msg('新增失败', {icon: 5,time: 1000});
				}
			});
		});
	});
	
	/**
	 * 修改分类方法
	 */
	$(".type-item").on('click','.revise',function(e){
		var id = $(this).parent().parent().attr('data-id');
		layer.prompt({title: '修改商品分类', formType: 0, value: $(e.target.parentElement).prev().prev().text()}, function(categoryName, index){
			$.ajax({
				type: "post",
				url: '/diconet/productCategory/update',
				data: {
					id : id,
					categoryName : categoryName
				},
				dataType: 'json',
				success: function(data){
					if(data.success == 'T'){
						layer.close(index);
						var categoryName = $("#categoryName").val();
				    	loadComment(page,rows,categoryName);
					}else{
						layer.msg(data.message, {icon: 5,time: 1000});
					}
				},
				error: function(data){
					layer.msg('修改失败', {icon: 5,time: 1000});
				}
			});
		});
	});
	
	/**
	 * 删除分类方法
	 */
	$(".type-item").on('click','.delete',function(){
		var id = $(this).parent().parent().attr('data-id');
		layer.confirm('是否要删除分类信息？', {
			btn: ['删除','取消'], //按钮
			title: '提示'
		}, function(index){
			$.ajax({
				type: "post",
				url: '/diconet/productCategory/remove',
				data: {
					id : id
				},
				dataType: 'json',
				success: function(data){
					layer.close(index);
					var categoryName = $("#categoryName").val();
			    	loadComment(page,rows,categoryName);
				},
				error: function(data){
					layer.msg('删除失败', {icon: 5,time: 1000});
				}
			});
		});
	});
	
	// 搜索框搜索
    $('.search-box').on('click','span',function(){
    	var categoryName = $("#categoryName").val();
    	loadComment(page,rows,categoryName);
    });
	
	$('.tab').on('click','div',function(){
		if($(this).html()=='商品分类'){
			window.location.href = path + "/distributor/product_set";
		}else if($(this).html()=='商品品牌'){
			window.location.href = path + "/distributor/product_brand";
		}else if($(this).html()=='商品单位'){
			window.location.href = path + "/distributor/product_unit";
		}
	});
	loadComment(page, rows, '');
//	点击展开分类
	$('.type-item').on('click','h5',function(){
		$(this).parent('.type').siblings('ul').slideToggle("fast");
	})
}

/**
 * 加载列表数据
 * @param page
 * @param rows
 */
function loadComment(page, rows,categoryName) {
	$.ajax({
		type: "post",
		url: '/diconet/productCategory/findAllProdCategory',
		data: {
//			page : page,
//			rows : rows,
			categoryName : categoryName
		},
		dataType: 'json',
		success: function(data){
			var level1 = "";
//			console.log(data.rows);
			if(data) {
				for(var i=0; i<data.data.length; i++) {
					var comment = data.data[i];
					var date1=comment.publishDate == null?'':comment.publishDate;
					if(data.data[i].parentId === ""){
						level1 += '<li class="product-item">';
						level1 += '<div class="type clearfix" data-id='+comment.id+'>';
						level1 += '<h5 class="fl pointer">'+comment.categoryName+'</h5>';
						level1 += '<div class="btn-box fr"><button name="'+comment.level+'" class="add">新增子类</button><button class="revise">修改</button><button class="delete">删除</button></div>';
						level1 += '</div>';
						level1 += '<ul class="level2">';
						level1 += '</ul>';
						level1 += '</li>';
					}
				}
				$('.type-item').html(level1);
				$($('.product-item')).each(function(){
					var id = $(this).find('.type').attr('data-id');
					var level2 = "";
					for(var i=0; i<data.data.length; i++) {
						var comment = data.data[i];
						var date1=comment.publishDate == null?'':comment.publishDate;
						if(data.data[i].parentId === id){
							level2 += '<li class="product-item">';
							level2 += '<div class="type clearfix" data-id='+comment.id+'>';
							level2 += '<h5 class="fl pointer">'+comment.categoryName+'</h5>';
							level2 += '<div class="btn-box fr"><button name="'+comment.level+'" class="add">新增子类</button><button class="revise">修改</button><button class="delete">删除</button></div>';
							level2 += '</div>';
							level2 += '<ul class="level3">';
							level2 += '</ul>';
							level2 += '</li>';
						}
					};
					$(this).children('.level2').html(level2);
				});
				$($('.level2 .product-item')).each(function(){
					var id = $(this).find('.type').attr('data-id');
					var level3 = "";
					for(var i=0; i<data.data.length; i++) {
						var comment = data.data[i];
						var date1=comment.publishDate == null?'':comment.publishDate;
						if(data.data[i].parentId === id){
							level3 += '<li class="product-item last-level">';
							level3 += '<div class="type clearfix" data-id='+comment.id+'>';
							level3 += '<h5 class="fl">'+comment.categoryName+'</h5>';
							level3 += '<div class="btn-box fr"><button class="revise">修改</button><button class="delete">删除</button></div>';
							level3 += '</div>';
							level3 += '</li>';
						}
					};
					$(this).children('.level3').html(level3);
				});
//				var dataPage = data.page;
//				var pageContent="";
//				if(dataPage.firstPage) {
//					pageContent += '<span class="disabled">首页</span>';
//					pageContent += '<span class="disabled">上一页</span>';
//				} else {
//					pageContent += '<a href="javascript:loadComment('+dataPage.first+','+rows+',\''+categoryName+'\');">首页</a>';
//					pageContent += '<a href="javascript:loadComment('+dataPage.prev+','+rows+',\''+categoryName+'\');">上一页</a>';
//				}
//				var maxPage = dataPage.page+2 < dataPage.last?dataPage.page+2:dataPage.last;
//				if(dataPage.page >= 4 && dataPage.page + 2 < dataPage.last){
//					for(var i=dataPage.page-2; i<= maxPage; i++) {
//						if(i == dataPage.page) {
//							pageContent += '<span class="current">'+i+'</span>';
//						} else {
//							pageContent += '<a href="javascript:loadComment('+i+','+rows+',\''+categoryName+'\');">'+i+'</a>';
//						}
//					}
//				}else if(dataPage.page > 4 && dataPage.page + 2 >= dataPage.last){
//					for(var i=dataPage.last-4; i<=dataPage.last; i++) {
//						if(i == dataPage.page) {
//							pageContent += '<span class="current">'+i+'</span>';
//						} else {
//							pageContent += '<a href="javascript:loadComment('+i+','+rows+',\''+categoryName+'\');">'+i+'</a>';
//						}
//					}
//				}else if(dataPage.last < 5){
//					for(var i=dataPage.first; i<=dataPage.last; i++) {
//						if(i == dataPage.page) {
//							pageContent += '<span class="current">'+i+'</span>';
//						} else {
//							pageContent += '<a href="javascript:loadComment('+i+','+rows+',\''+categoryName+'\');">'+i+'</a>';
//						}
//					}
//				}else{
//					for(var i=dataPage.first; i<=5; i++) {
//						if(i == dataPage.page) {
//							pageContent += '<span class="current">'+i+'</span>';
//						} else {
//							pageContent += '<a href="javascript:loadComment('+i+','+rows+',\''+categoryName+'\');">'+i+'</a>';
//						}
//					}
//				}
//				if(dataPage.lastPage) {
//					pageContent += '<span class="disabled">下一页</span>';
//					pageContent += '<span class="disabled">尾页</span>';
//				} else {
//					pageContent += '<a href="javascript:loadComment('+dataPage.next+','+rows+',\''+categoryName+'\');">下一页</a>';
//					pageContent += '<a href="javascript:loadComment('+dataPage.last+','+rows+',\''+categoryName+'\');">尾页</a>';
//				}
//			}
//			$(".quotes").html(pageContent);
			}
		},
		error : function(data){
			layer.msg('请求失败', {time:500,icon:5}); 
		}
	});
}

function deleteUnit(id){
	$.ajax({
		type:"post",
		url:"findProductByUnit",
		data:{"id":id},
		dataType:"json",
		success:function(data){
			if(data == 0){
				console.log("可以删");
				$.ajax({
		    		  type: 'POST',
		    		  url: "delUnit",
		    		  data: {id:objId},
		    		  dataType:'text',
		    		  success: function (isSuccess){
		    			  $('input[value="'+objId+'"]').parent().remove();
		    		  }
		    	});
			}else if(data == 1){
				console.log("不能删");
				alert("不能删除");
			}
		}
	})
}

init();